/**
 * 
 */
package net.yangying.androidframework.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.util.DisplayMetrics;
import android.util.Log;

/**
 * @author yangying
 * @date 2013-6-13 下午3:12:09
 * @version 1.0
 *
 */
public class PackageUtil {


	/**
	 * 需要隐藏api PackageParser 
	 * @param context
	 */
/*	public static android.content.pm.PackageInfo parsePackage(String archiveFilePath, int flags) {

		PackageParser packageParser = new PackageParser(archiveFilePath);
		DisplayMetrics metrics = new DisplayMetrics();
		metrics.setToDefaults();
		final File sourceFile = new File(archiveFilePath);
		PackageParser.Package pkg = packageParser.parsePackage(sourceFile,
				archiveFilePath, metrics, 0);
		if (pkg == null) {
			return null;
		}
		packageParser.collectCertificates(pkg, 0);
		return PackageParser.generatePackageInfo(pkg, null, flags, 0, 0, null, null);
	}*/
	public static void getSingInfo(String packageName, Context context) {
		try {
			PackageInfo packageInfo = context.getPackageManager().getPackageInfo(
					packageName, PackageManager.GET_SIGNATURES);
			Signature[] signs = packageInfo.signatures;
			Signature sign = signs[0];
			parseSignature(sign.toByteArray());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void parseSignature(byte[] signature) {
		try {
			CertificateFactory certFactory = CertificateFactory
					.getInstance("X.509");
			X509Certificate cert = (X509Certificate) certFactory
					.generateCertificate(new ByteArrayInputStream(signature));
			String pubKey = cert.getPublicKey().toString();
			Log.i("tag","pubKey:"+pubKey);
			String signNumber = cert.getSerialNumber().toString();
			Log.i("tag","signNumber:"+signNumber);
		} catch (CertificateException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询手机内的应用
	 * @param context
	 * @param appType 0:全部应用；1系统应用；2:非系统应用
	 * @return
	 */
	public static List<PackageInfo> getPackageList(Context context, int appType) {
		List<PackageInfo> apps = new ArrayList<PackageInfo>();
		PackageManager pManager = context.getPackageManager();
		// 获取手机内所有应用
		List<PackageInfo> paklist = pManager.getInstalledPackages(0);
		for (int i = 0; i < paklist.size(); i++) {
			PackageInfo pak = (PackageInfo) paklist.get(i);
			switch (appType) {
				case 0:
					apps.add(pak);
					break;
				case 1:
					if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) > 0) {
						// system applications
						apps.add(pak);
					}
					break;
				case 2:
					if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
						// customs applications
						apps.add(pak);
					}
					break;
				default:
					apps.add(pak);
					break;
			}
		}
		return apps;
	}
    
}
